import * as React from "react";
import { ImagePack, ImageDefault } from "@tools/ImagePack";
import { calendar as Calendar, ISolarParent } from "@tools/SolarTerms";
import { ImageScreen } from "@ui/ImageScreen";
// import { ConsoleLog , ToastLog } from './tools/Decorators';
import { DateFormat, IDateOb } from '@tools/DateFormat';
import { RoutersConfig } from "@routers/RoutersConfig";
import "./App.scss";
import { Routers } from '@routers/Routers';
import { observer , inject } from 'mobx-react';
import DevTools from "mobx-react-devtools";

let solar: ISolarParent = {
    lYear: 0,
    lMonth: 0,
    lDay: 0,
    gzYear: '',
    gzMonth: '',
    gzDay: '',
    nWeek: 0,
    Term: '',
    TermPath: ''
};

@inject("Global")
@observer
class App extends React.Component<{ Global?: IGlobalTypes.IGlobal },{}> {

    public componentWillMount() {
        const first: string | null = window.sessionStorage.getItem("first");
        if (!!first) {
            this.props.Global!.setFirst(false);
        }
    }

    public componentDidMount() {
        const first: string | null = window.sessionStorage.getItem("first");
        const datePart: IDateOb = new DateFormat().datePart(new Date());
        const solarInfo: ISolarParent | number = Calendar.solar2lunar(datePart.year, datePart.month, datePart.date);
        const solarInfoV: ISolarParent = typeof solarInfo === 'number' ? solar : solarInfo;
        solar = solarInfoV;
        const src: string = !solar.TermPath ? ImageDefault.defaultSolar.uri : ImagePack[solar.TermPath].uri;
        this.props.Global!.setSrc(src);
        if (!first) {
            setTimeout(() => {
                this.showPage(false);
            }, 3000)
        }
    }

    public showPage(mark: boolean): void {
        this.props.Global!.setFirst(mark);
        window.sessionStorage.setItem('first', "true");
    }

    // @ConsoleLog('show me your log')
    // @ToastLog({content: 'show me your log by toast'})
    public render(): JSX.Element {
        const { first } = this.props.Global!;
        return (
            <div className="App">
                {
                    first ? <ImageScreen /> : <Routers config={ RoutersConfig } level={ 1 } />
                }
                <DevTools />
            </div>
        );
    }

}

export default App;
